<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title><?php echo $this->siteConfig['title']; ?></title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/static/default/css/style.css">
</head>
</head>

<body>
    <div class="frame" id="frame">
        <header class="titlebar">
            <div class="dot" title="Close" onclick="location.reload()"></div>
            <div class="dot" title="Minimize" onclick="minimize()"></div>
            <div class="dot" title="Fullscreen" onclick="toggleFullscreen()"></div>
        </header>

        <main class="content">
            <?php
            // 加载配置文件
            $selectedPartition = isset($_GET['partition']) ? $_GET['partition'] : null;
            $siteConfig = $this->siteConfig;
            $blogList = $this->blogList;
            $partitions = $siteConfig['partitions'] ?? [];

            // 筛选帖子
            $filteredPosts = [];
            if ($selectedPartition) {
                $filteredPosts = array_filter($blogList, function ($post) use ($selectedPartition) {
                    return $post['partition'] == $selectedPartition;
                });
            } else {
                $filteredPosts = $blogList;
            }

            // 先按置顶状态排序，再按时间降序排序
            usort($filteredPosts, function ($a, $b) {
                $topA = isset($a['top']) && $a['top'] === true ? 1 : 0;
                $topB = isset($b['top']) && $b['top'] === true ? 1 : 0;

                if ($topA !== $topB) {
                    return $topB - $topA; // 置顶的排在前面
                }

                return strtotime($b['time']) - strtotime($a['time']); // 然后按时间降序
            });


            // 获取分区介绍
            $partitionTitle = 'All Posts';
            $partitionIntro = '';
            if ($selectedPartition) {
                $partition = current(array_filter($partitions, function ($p) use ($selectedPartition) {
                    return $p['id'] == $selectedPartition;
                }));
                if ($partition) {
                    $partitionTitle = $partition['title'];
                    $partitionIntro = $partition['description'] ?? '';
                }
            }
            ?>

            <!-- Partition Introduction -->
            <h1>// <?php echo $partitionTitle; ?></h1>
            <?php if ($partitionIntro): ?>
                <p><?php echo $partitionIntro; ?></p>
            <?php endif; ?>

            <!-- Partitions -->
            <h2>// Partitions</h2>
            <div class="partitions" style="display:flex;gap:12px;margin-bottom:20px;">
                <?php foreach ($partitions as $partition): ?>
                    <a href="/Partitions/<?php echo $partition['id']; ?>.html" style="padding:6px 12px;background:var(--glass);border-radius:6px;text-decoration:none;color:var(--text);" class="partition-btn <?php echo $selectedPartition == $partition['id'] ? 'active' : ''; ?>"><?php echo $partition['title']; ?></a>
                <?php endforeach; ?>
            </div>

            <!-- Blog Posts -->
            <h2>// Posts</h2>
            <div class="posts-container">
                <?php if (empty($filteredPosts)): ?>
                    <p>该分区暂无帖子</p>
                <?php else: ?>
                    <?php foreach ($filteredPosts as $post): ?>
                        <div class="card" style="margin-bottom:16px;">
                            <div style="display:flex;justify-content:space-between;align-items:center;">
                                <h3><a href="/posts/<?php echo $post['id']; ?>.html"><?php echo $post['title']; ?></a></h3>
                                <?php if (isset($post['top']) && $post['top'] === true): ?>
                                    <span style="background:var(--accent);color:white;padding:2px 8px;border-radius:4px;font-size:0.7em;">置顶</span>
                                <?php endif; ?>
                            </div>
                            <p><?php echo $post['author']; ?> | <?php echo $post['time']; ?></p>
                            <div style="margin-top:8px;">
                                <?php foreach ($post['tags'] as $tag): ?>
                                    <span style="font-size:0.8em;background:rgba(255,255,255,0.1);padding:2px 8px;border-radius:12px;margin-right:4px;"><?php echo $tag; ?></span>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </main>
    </div>

    <script src="/static/default/js/script.js"></script>
</body>

</html>